import { reqcardel, reqcarlist, reqcaredit } from '../../request/api';
import { getUserInfo } from './login'
import { Toast } from 'antd-mobile';

//状态
const initState = {
    shops: [],
    isAllcheck: false,
    arr: ['全部', '降价', '常卖', '分类']
}
let isRequest = true;
//actionType
const TYPES = {
    SHOP_CHANGE_SHOPS: 'SHOP_CHANGE_SHOPS',
    SHOP_CHANGE_ISALLCHECK: 'SHOP_CHANGE_ISALLCHECK'
}

//修改state reducer 
const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.SHOP_CHANGE_SHOPS:
            return {
                ...state,
                shops: action.shops
            }
        case TYPES.SHOP_CHANGE_ISALLCHECK:
            return {
                ...state,
                isAllcheck: action.isAllcheck
            }
        default:
            return state;
    }
}

//做逻辑和异步的acitons actionCreators
export const actions = {
    changeShops: (shops) => ({ type: TYPES.SHOP_CHANGE_SHOPS, shops }),
    // 请求购物车列表
    requestCartList: () => (dispatch, getState) => {
        let uid = getUserInfo(getState()).uid;
        console.log(uid)
        reqcarlist({ uid }).then(res => {
            if (res.data.code == 200) {
                //   合并数据
                let arr1 = res.data.list[0].content ? res.data.list[0].content : [];
                let arr2 = res.data.list[1].content ? res.data.list[1].content : [];
                let arr = [...arr1, ...arr2];
                // 仓库原数据
                let oldShops = getShops(getState());
                arr.forEach(item => {
                    let obj = oldShops.find(i => i.id == item.id);
                    item.ischeck = obj ? obj.ischeck : false;
                })
                // 修改shops
                dispatch(actions.changeShops(arr));
                // 修改全选
                dispatch(actions.changeIsAllCheck(arr.length > 0 ? arr.every(item => item.ischeck) : false))
                // 下一次请求可以开始
                isRequest = true;
            }
        })
    },
    // 修改全选
    changeIsAllCheck: (isAllcheck) => ({ type: TYPES.SHOP_CHANGE_ISALLCHECK, isAllcheck }),
    // 删除
    Del: (id) => (dispatch, getState) => {
        reqcardel({ id }).then(res => {
            if (res.data.code == 200) {
                Toast.show({
                    content: res.data.msg
                })
                dispatch(actions.requestCartList())
            }
        })
    },
    // ++
    add: (id) => (dispatch, getState) => {
        reqcaredit({ id, type: 2 }).then(res => {
            if (res.data.code == 200) {
                dispatch(actions.requestCartList())
            }
        })
    },
    // --
    sub: (id, num) => (dispatch, getState) => {
        if (num <= 1) {
            Toast.show({
                content: '宝贝数量不能再少了'
            })
            return;
        }
        if (isRequest) {
            isRequest = false;
            reqcaredit({ id, type: 1 }).then(res => {
                if (res.data.code == 200) {
                    dispatch(actions.requestCartList())
                }
            })
        }
    },
    // 全选
    allCheck: (e) => (dispatch, getState) => {
        // 修改全选
        dispatch(actions.changeIsAllCheck(e));
        // 修改列表单选框
        let shops = getShops(getState());
        shops.forEach(item => {
            item.ischeck = e;
        })
        dispatch(actions.changeShops([...shops]))
    },
    // 操作单个是否选中
    changeOne: (e, index) => (dispatch, getState) => {
        let shops = getShops(getState());
        shops[index].ischeck = e;
        // 修改全选
        dispatch(actions.changeIsAllCheck(shops.every(item => item.ischeck)));
        // 修改购物车列表
        dispatch(actions.changeShops([...shops]))
    }
}

//导出数据 Selectors 
export const getShops = state => state.shop.shops;
export const getIsAllCheck = state => state.shop.isAllcheck;
export const getArr = state => state.shop.arr;
// 计算总价
export const getTotalPrice = state => {
    let shops = getShops(state);
    let sum = 0;
    shops.forEach(item => {
        if (item.ischeck) {
            sum += item.price * item.num;
        }
    })
    return sum;
}
//导出reducer 
export default reducer;